1. Field of the Invention
The present invention relates to techniques for ensuring exclusive access to global resources within a computer system. More specifically, the present invention relates to a method and an apparatus for providing dynamic locks for global resources in a multithreaded computing environment.
2. Related Art
Modern computer operating systems typically support concurrent execution of multiple threads. This enables a computer system to execute multiple applications simultaneously, which generally leads to better utilization of resources and higher system throughput. However, multi-threaded execution can cause data integrity problems if the multiple threads are allowed to concurrently access shared system resources, such as shared files or global data structures.
In order to alleviate this problem, operating systems typically make use of mutual exclusion locks (mutexes) to synchronize accesses shared resources. A thread wishing to access a shared resource must first acquire a mutex for the resource. Once the mutex has been acquired, the thread has exclusive access to the resource, and can make changes to the resource without interference by other threads. The other threads are only able to access the resource after the mutex has been released.
Mutexes are often incorporated into global data structures. For example, Table 1 illustrates code that defines an exemplary data structure including a mutex.
TABLE 1typedef struct _resource {mutex *lock;<. . .>data structure members<. . .>} Resource;The first field within this data structure is a “lock field,” which indicates whether or not the resource is currently “locked” by a thread.
Although this type of mutex can be used to control access to the resource, using this type of mutex has drawbacks. When the resource is locked, the entire resource is locked. This can create a contention problem if the current thread only accesses a small portion of the resource, and other threads are waiting to access other portions of the resource. Moreover, the mutex takes up space in the data structure, even if the mutex is not used.
Hence, what is needed is a method and an apparatus for locking shared resources within a computer system without the problems described above.